// https://leetcode.cn/problems/average-of-levels-in-binary-tree/
// Created by ade on 2022/8/10.
//
#include <iostream>
#include <vector>


using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector <vector<long long>> res;

    vector<double> averageOfLevels(TreeNode *root) {
        dfs(root, 0);
        vector<double> r = {};
        for (auto item:res) {
            r.push_back(double(accumulate(item.begin(), item.end(), 0.0)) / item.size());
        }
        return r;
    }

    void dfs(TreeNode *node, int index) {
        if (!node) return;
        if (res.size() <= index) res.push_back({});
        res[index].push_back(node->val);
        dfs(node->left, index + 1);
        dfs(node->right, index + 1);
    }
};

int main() {

}